<?php

class sfApplySettingsForm extends sfGuardUserProfileForm
{
  public function configure()
  {
    parent::configure();
    $this->removeFields();

    // We're editing the user who is logged in. It is not appropriate
    // for the user to get to pick somebody else's userid, or change
    // the validate field which is part of how their account is
    // verified by email. Also, users cannot change their email
    // addresses as they are our only verified connection to the user.

    $this->widgetSchema->setLabels(
            array(
                'firstname' => 'First Name',
                'lastname' => 'Last name') );
    $this->widgetSchema->moveField('firstname', sfWidgetFormSchema::FIRST);
    $this->widgetSchema->moveField('lastname', sfWidgetFormSchema::AFTER, 'firstname');

    $this->widgetSchema->setNameFormat('sfApplySettings[%s]');
    $this->widgetSchema->setFormFormatterName('list');

    $this->setValidator( 'firstname' , new sfValidatorApplyFirstname() );
    $this->setValidator( 'lastname', new sfValidatorApplyLastname() );

    $user = sfContext::getInstance()->getUser()->getGuardUser();
    $licensee = Doctrine::getTable('Licensee')->getMemberUserInfo($user->getId())->fetchOne();

    $licenseeForm = new LicenseeFrontendForm($licensee);
    $this->embedForm('licensee', $licenseeForm);

    $clubForm = new ClubForm();
    $this->embedForm('club', $clubForm);

    $this->setWidget('save_club', new sfWidgetFormInputHidden());
    $this->setDefault('save_club', 'false');
    $this->setValidator('save_club', new sfValidatorBoolean(array('required' => true)));
  }

  public function saveEmbeddedForms($con = null, $forms = null){
      $embedForms = $this->getEmbeddedForms();

      $licenseeForm = $embedForms['licensee'];
      $licenseeForm->getObject()->setLastname( $this->getObject()->getLastname() );
      $licenseeForm->getObject()->setFirstname( $this->getObject()->getFirstname() );

      if(isset($embedForms['club'])){
          $licenseeForm->getObject()->setClub( $embedForms['club']->getObject() );
      }
      
      return parent::saveEmbeddedForms($con, $forms);
  }

  public function bind(array $taintedValues = null, array $taintedFiles = null){
      if($taintedValues['save_club'] == 'false'){
         unset($taintedValues['club']);
         unset($this->embeddedForms['club']);
         unset($this->validatorSchema['club']);
         unset($taintedFiles['club']);
      }
      parent::bind($taintedValues, $taintedFiles);
  }

  protected function removeFields()
  {
    unset(
        $this['user_id'],
        $this['validate'],
        $this['email'],
        $this['email_new'],
        $this['validate_at'],
        $this['id'],
        $this['created_at'],
        $this['updated_at']
    );
  }
}